from uuid import UUID

from PyQt5.QtWidgets import QVBoxLayout, QWidget, QTreeView, QAbstractItemView
from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtGui import QStandardItemModel

from data import database

class TrackList(QWidget):
    TRACK_TITLE, TRACK_ARTIST, TRACK_ALBUM, TRACK_GENRE, TRACK_UUID, TRACK_PATH = range(6)

    def __init__(self):
        super().__init__(None)

        layout = QVBoxLayout()

        self.track_list = QTreeView()
        self.track_list.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.track_list_model = QStandardItemModel(0, 6, self)
        self.track_list.setModel(self.track_list_model)
        self.track_list_model.setHeaderData(self.TRACK_TITLE, Qt.Horizontal, "Title")
        self.track_list_model.setHeaderData(self.TRACK_ARTIST, Qt.Horizontal, "Artist")
        self.track_list_model.setHeaderData(self.TRACK_ALBUM, Qt.Horizontal, "Album")
        self.track_list_model.setHeaderData(self.TRACK_GENRE, Qt.Horizontal, "Genre")
        self.track_list_model.setHeaderData(self.TRACK_UUID, Qt.Horizontal, "UUID")
        self.track_list_model.setHeaderData(self.TRACK_PATH, Qt.Horizontal, "Path")
        layout.addWidget(self.track_list)
        self.refresh_track_list()

        self.setLayout(layout)

    def refresh_track_list(self):
        self.track_list_model.removeRows(0, self.track_list_model.rowCount())
        for uuid in sorted(database['music'], key=lambda x: str(database['music'][x]['title']) if database['music'][x]['title'] else 'Unknown', reverse=True):
            self.track_list_model.insertRow(0)
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_TITLE), database['music'][uuid]['title'] if database['music'][uuid]['title'] else 'Unknown')
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_ARTIST), str(list(database['artists'][artist_uuid]['name'] for artist_uuid in database['music'][uuid]['artists'])) if database['music'][uuid]['artists'] else 'Unknown')
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_ALBUM), ((database['albums'][database['music'][uuid]['album']]['name'] if database['albums'][database['music'][uuid]['album']]['name'] else database['music'][uuid]['album']) + ' by ' + (database['artists'][database['albums'][database['music'][uuid]['album']]['artist']]['name'] if database['albums'][database['music'][uuid]['album']]['artist'] else 'Unknown')) if database['music'][uuid]['album'] else 'Unknown')
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_GENRE), (database['genres'][database['music'][uuid]['genre']]['name'] if database['genres'][database['music'][uuid]['genre']]['name'] else database['music'][uuid]['genre']) if database['music'][uuid]['genre'] else 'Unknown')
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_UUID), str(uuid))
            self.track_list_model.setData(self.track_list_model.index(0, self.TRACK_PATH), database['music'][uuid]['path'])

        self.track_list.resizeColumnToContents(self.TRACK_TITLE)
        self.track_list.resizeColumnToContents(self.TRACK_ARTIST)
        self.track_list.resizeColumnToContents(self.TRACK_ALBUM)
        self.track_list.resizeColumnToContents(self.TRACK_GENRE)
        self.track_list.resizeColumnToContents(self.TRACK_UUID)
        self.track_list.resizeColumnToContents(self.TRACK_PATH)

    def uuid_from_index(self, index: QModelIndex) -> UUID:
        index = self.track_list_model.index(index.row(), self.TRACK_UUID)
        item = self.track_list_model.itemFromIndex(index)
        uuid = item.text()
        return UUID(uuid)
